import { HttpParams } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { MenuApiService } from 'src/app/api/menu-api.service';
import { Menu } from 'src/app/model/menu';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.less', '../../../styles/container.css']
})
export class MenuComponent implements OnInit {

  menuList: Array<Menu>;
  tableLoading = false;
  pageIndex = 1;
  pageSize = 10;
  total = 0;
  isInfoVisible = false;
  inforFormTitle = "添加菜单";
  showStatusLoadingStatus = new Map<number, boolean>();
  menuFrom: FormGroup;
  parentId = 0;
  parentMenuOption = new Array<{value: number, label: string}>()
  constructor(
    private menuApi: MenuApiService,
    private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router,
    private fb: FormBuilder,
  ) {
    this.fb.group({
      title: [],
      parentId: [],
      name: [],
      hidden: [],
      sort: [],
    })
  }

  ngOnInit(): void {
    this.route.queryParams.subscribe(
      param => {
        this.parentId = param.parentId ? param.parentId : 0;
        this.getMenuFromServer(this.parentId);
      }
    )
  }

  handleAdd() {
    this.inforFormTitle = '添加菜单';
    this.isInfoVisible = true;
  }
  handlePageIndexChange() { }
  handlePageSizeChange() { }
  handleCancel() {
    this.isInfoVisible = false;
  }
  handleModalOk() {
    this.isInfoVisible = false
  }
  handleaShowStatusClick() { }
  handleGoDownLevel(id: number) {
    let url = this.router.parseUrl('/ums/menu');
    url.queryParams = { parentId: id };
    this.router.navigateByUrl(url);
  }
  handleEdit(data: Menu) {
    this.setFormValue(data);
    this.inforFormTitle = '编辑菜单';
    this.isInfoVisible = true;
  }
  handleDelete(id: number) {
    this.menuApi.delete([id]).subscribe(
      response => {
        this.message.success('菜单删除成功');
        this.getMenuFromServer(this.parentId);
      },
      err => {
        this.message.error('删除菜单错误');
        console.error('删除菜单错误', err);
      }
    )
  }
  cancelDelete() { }

  private getMenuFromServer(parentId: number) {
    this.tableLoading = true;
    let params = new HttpParams({
      fromObject: {
        'pageNum': this.pageIndex.toString(),
        'pageSize': this.pageSize.toString()
      }
    })
    this.menuApi.fetchListInParent(parentId, params).subscribe(
      response => {
        this.menuList = response.list;
        this.total = response.total;
        this.tableLoading = false;
      },
      err => {
        this.message.error('获取数据错误');
        this.tableLoading = false;
      }
    )
  }

  private setFormValue(data: Menu) {
    for (const key in data) {
      if (Object.prototype.hasOwnProperty.call(data, key)) {
        if(this.menuFrom.contains(key)){
          this.menuFrom.controls[key].setValue(data[key]);
        }
      }
    }
  }

}
